﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using ltModel;
using Telerik.Web.UI;

public partial class ltMan_news_NewsOperate : AdminBaseUserControl
{
    ltModel.BLL.article_content atbl = new ltModel.BLL.article_content();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindBaseData();
            string id = Request.QueryString["id"];
            if (Str.IsInt(id))
            {
                ltModel.Model.article_content atbo = atbl.GetModel(Int32.Parse(id));
                if (atbo != null)
                {
                    News_title.Text = atbo.Title;
                    News_author.Text = atbo.author;
                    News_Source.Text = atbo.Source;
                    News_keyword.Text = atbo.Keyword;
                    News_titlepic.Value = atbo.TitlePic;
                    News_content.Value = atbo.Content;
                    News_dat.DbSelectedDate = atbo.Dat;
                    News_jg.Text = atbo.fb_jg;
                    News_wh.Text = atbo.fb_wh;
                    News_ss_dat.DbSelectedDate = atbo.ss_dat;
                    string wjm = atbo.All_wjm;
                    if (!string.IsNullOrEmpty(wjm))
                    {
                        CheckBoxList cbxl_many = comboMany.Items[0].FindControl("cbxl_many") as CheckBoxList;
                        for (int i = 0; i < cbxl_many.Items.Count; i++)
                        {
                            if (wjm.IndexOf(("|" + cbxl_many.Items[i].Value + "|")) >= 0)
                                cbxl_many.Items[i].Selected = true;
                        }
                        cbx_many.Checked = true;
                    }
                    RadTreeView treeview = News_class.Items[0].FindControl("RadTreeView1") as RadTreeView;
                    RadTreeNode node = treeview.FindNodeByValue(atbo.Classid.ToString());
                    node.Selected = true;
                }
            }
            else
            {
                News_dat.SelectedDate = News_ss_dat.SelectedDate = DateTime.Now;
                News_author.Text = CurrentUser.truename;
            }
        }
    }
    void BindBaseData()
    {
        ltModel.BLL.article_class acbl = new ltModel.BLL.article_class();

        string where = string.Empty;
        if (!CurrentUser.IsManager)
        {
            where = string.Format(" Class_id in ({0}) ", CurrentUserGroup.GetLmQx());
        }

        DataTable cdt = acbl.GetList(where).Tables[0];
        int k = 0;
        CheckBoxList cbxl_many = comboMany.Items[0].FindControl("cbxl_many") as CheckBoxList;
        for (int j = 0; j < cdt.DefaultView.Count; j++)
        {
            if (cdt.DefaultView[j]["sub_class"].ToString().ToLower() == "false" || cdt.DefaultView[j]["sub_class"].ToString() == "0")
            {
                if (cdt.DefaultView[j]["ceng_num"].ToString().IndexOf("0|19|") < 0)
                {
                    cbxl_many.Items.Add(Str.LeftString(cdt.DefaultView[j]["classname"].ToString(), 7));
                    cbxl_many.Items[k].Value = cdt.DefaultView[j]["Class_id"].ToString();
                    k++;
                }
            }
        }
        if (!string.IsNullOrEmpty(where))
        {
            where = " WHERE " + where;
        }

        RadTreeView treeview = News_class.Items[0].FindControl("RadTreeView1") as RadTreeView;
        treeview.DataSource = SqlHelper.Instance.ExecuteReader("select * from lt_article_class " + where);
        treeview.DataBind();
        treeview.ExpandAllNodes();
        bool hasSelected = false;
        foreach (RadTreeNode item in treeview.GetAllNodes())
        {
            if (item.Nodes.Count > 0)
            {
                item.Enabled = false;
                item.ForeColor = System.Drawing.Color.Black;
                item.Font.Italic = true;
            }
            else
            {
                if (!hasSelected)
                {
                    hasSelected = true;
                    item.Selected = true;
                }
            }
        }
    }
    bool CheckRequiredFields(bool inEdit)
    {
        bool v = true;
        if (string.IsNullOrEmpty(News_title.Text))
        {
            infoPanel.AddErrorInfo("新闻标题必填！");
            OperateLog.AddFailedLog(CurrentUser.user_id, Request.UserHostAddress, "新闻管理->" + (inEdit ? "添加" : "修改") + ";新闻标题为空");
            v = false;
        }
        if (string.IsNullOrEmpty(News_content.Value))
        {
            infoPanel.AddErrorInfo("新闻内容必填！");
            OperateLog.AddFailedLog(CurrentUser.user_id, Request.UserHostAddress, "新闻管理->" + (inEdit ? "添加" : "修改") + ";新闻内容为空");
            v = false;
        }
        if (!isGov)
        {
            if (string.IsNullOrEmpty(News_author.Text))
            {
                infoPanel.AddErrorInfo("新闻作者必填！");
                OperateLog.AddFailedLog(CurrentUser.user_id, Request.UserHostAddress, "新闻管理->" + (inEdit ? "添加" : "修改") + ";新闻作者为空");
                v = false;
            }
            if (string.IsNullOrEmpty(News_Source.Text))
            {
                infoPanel.AddErrorInfo("新闻来源必填！");
                OperateLog.AddFailedLog(CurrentUser.user_id, Request.UserHostAddress, "新闻管理->" + (inEdit ? "添加" : "修改") + ";新闻来源为空");
                v = false;
            }
        }
        else
        {
            if (string.IsNullOrEmpty(News_jg.Text))
            {
                infoPanel.AddErrorInfo("发布机构必填！");
                OperateLog.AddFailedLog(CurrentUser.user_id, Request.UserHostAddress, "新闻管理->" + (inEdit ? "添加" : "修改") + ";发布机构为空");
                v = false;
            }
            if (string.IsNullOrEmpty(News_wh.Text))
            {
                infoPanel.AddErrorInfo("发布文号必填！");
                OperateLog.AddFailedLog(CurrentUser.user_id, Request.UserHostAddress, "新闻管理->" + (inEdit ? "添加" : "修改") + ";发布文号为空");
                v = false;
            }

        }
        return v;
    }
    protected void RadSave_Click(object sender, EventArgs e)
    {
        string id = Request.QueryString["id"];
        bool inEdit = Str.IsInt(id);
        if (!CheckRequiredFields(inEdit))
            return;
        ltModel.Model.article_content atbo = null;
        if (inEdit)
            atbo = atbl.GetModel(Int32.Parse(id));
        else
            atbo = new ltModel.Model.article_content();

        string cs = "|";
        int si = 0;
        if (this.cbx_many.Checked)
        {
            CheckBoxList cbxl_many = comboMany.Items[0].FindControl("cbxl_many") as CheckBoxList;
            for (int a = 0; a < cbxl_many.Items.Count; a++)
            {
                if (cbxl_many.Items[a].Selected)
                {
                    cs = cs + cbxl_many.Items[a].Value + "|";
                    si++;
                }
            }
        }
        if (si == 0)
            cs = string.Empty;

        atbo.Title = DataProtector.ToSBC(this.News_title.Text.Trim());
        atbo.Classid = Int32.Parse(this.News_class.SelectedValue);
        atbo.author = DataProtector.ToSBC(this.News_author.Text.Trim());
        atbo.Source = DataProtector.ToSBC(this.News_Source.Text.Trim());
        atbo.Keyword = DataProtector.ToSBC(this.News_keyword.Text.Trim());
        atbo.TitlePic = DataProtector.ToSBC(this.News_titlepic.Value.Trim());
        atbo.Content = News_content.Value.Trim().Replace("'", "''");
        atbo.Dat = News_dat.SelectedDate.ToString();
        atbo.All_wjm = cs;
        atbo.All_wjs = si;
        atbo.One_wjm = atbo.Classid.ToString();

        atbo.fb_jg = DataProtector.ToSBC(News_jg.Text);
        atbo.fb_wh = DataProtector.ToSBC(News_wh.Text);
        atbo.ss_dat = News_ss_dat.SelectedDate.ToString();

        if (!inEdit)
        {
            atbo.Tj = "0";
            atbo.sh = "1";
            atbo.Hit = 0;
            atbo.faburen = CurrentUser.username;
            atbo.shr = "admin";
            atbo.focus = isGov ? "1" : "0";
            atbo.pid = 0;
            atbo.News_id = 0;
            atbo.Link = "";
            atbo.News_id = atbl.Add(atbo);
            if (atbo.News_id > 0)
            {
                atbl.UpdateManFab(CurrentUser.username);//修改发布量
                atbl.UpdateManSh("admin");//修改审核量
                infoPanel.AddSuccessInfo("添加成功");
                OperateLog.AddSuccessLog(CurrentUser.user_id, Request.UserHostAddress, "新闻管理-添加>" + atbo.Title);
            }
        }
        else
        {
            atbl.Update(atbo);
            infoPanel.AddSuccessInfo("修改成功");
            OperateLog.AddSuccessLog(CurrentUser.user_id, Request.UserHostAddress, "新闻管理-修改>" + atbo.Title);
        }

        //生成html
        string link = new BuilderNews(atbo).Build(atbo.Link);
        if (string.IsNullOrEmpty(atbo.Link) || link != atbo.Link)
            atbl.UpdateLink(atbo.News_id, link);

        AddScriptM("setTimeout(function(){if (top.treeWithTab){top.treeWithTab.deleteTabByUrl('" + Request.RawUrl + "');}else{location='newsList.aspx';}}, 300);");
    }
    protected bool isGov;

    public bool IsGov
    {
        set { isGov = value; }
    }

    private void SetRss(string cid, string nid)
    {
        string xmllink = "";
        string xmltitle = "";
        string xmlclass = "";
        string bh = "";
        string xmlran = nid;
        switch (cid)
        {
            case "71":
                xmllink = "http://www.xiapu.gov.cn/show-zt.aspx?nid=" + nid;
                xmltitle = "动态信息";
                xmlclass = "006";
                bh = "21";
                break;
            case "72":
                xmllink = "http://www.xiapu.gov.cn/show-gg.aspx?nid=" + nid;
                xmltitle = "公告公示";
                xmlclass = "007";
                bh = "31";
                break;
            case "75":
            case "76":
            case "77":
                xmllink = "http://www.xiapu.gov.cn/show-fg.aspx?nid=" + nid;
                xmltitle = "法规文件";
                xmlclass = "004";
                bh = "51";
                break;
            case "68":
                xmllink = "http://www.xiapu.gov.cn/show-rs.aspx?nid=" + nid;
                xmltitle = "人员任免";
                xmlclass = "005";
                bh = "61";
                break;
            case "115":
                xmllink = "http://www.xiapu.gov.cn/show-xz.aspx?nid=" + nid;
                xmltitle = "办事指南";
                xmlclass = "009";
                bh = "41";
                break;
        }
        if (xmllink != "" && xmltitle != "" && xmlclass != "")
        {

            if (xmlran.Length < 5)
            {
                for (int i = 1; i <= 5 - nid.Length; i++)
                    xmlran = xmlran + "0";
            }
            else
            {
                if (xmlran.Length > 5)
                    xmlran = xmlran.Substring(xmlran.Length - 5, 5);
            }

            ltModel.Rss rss = new ltModel.Rss();
            rss.XmlAuthor = "";
            rss.XmlClass = xmltitle;
            rss.XmlComments = "";
            rss.XmlDescription = "";
            rss.XmlGuid = "385" + xmlclass + DateTime.Now.Year.ToString() + bh + xmlran;
            rss.XmlStatus = "0";
            rss.XmlTempUrl = "/gb/zjeg_rss/template.xml";
            rss.XmlTitle = xmltitle;//acbo.Title;
            rss.XmlLink = xmllink;
            rss.RssCreate();
        }
    }
}